package com.ph.shopping.facade.permission.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.exception.BizException;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.mapper.ManageBankCardBindMapper;
import com.ph.shopping.facade.mapper.ManageBankCardInfoMapper;
import com.ph.shopping.facade.mapper.ManageBankCardLogMapper;
import com.ph.shopping.facade.permission.entity.ManageBankCardBind;
import com.ph.shopping.facade.permission.entity.ManageBankCardInfo;
import com.ph.shopping.facade.permission.entity.ManageBankCardLog;
import com.ph.shopping.facade.permission.service.IManageBankCardService;
import com.ph.shopping.facade.permission.vo.SessionUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @项目：phshopping-service-permission
 * @描述：
 * @作者： ShuHao
 * @创建时间：2017-03-15
 * @Copyright @2017 by ShuHao
 */
@Component
@Service(version = "1.0.0")
public class ManageBankCardService extends BaseService implements IManageBankCardService {

    private static final Logger logger = LoggerFactory.getLogger(ManageBankCardService.class);

    @Autowired
    ManageBankCardInfoMapper bankCardInfoMapper;

    @Autowired
    ManageBankCardBindMapper bankCardBindMapper;

    @Autowired
    ManageBankCardLogMapper bankCardLogMapper;


    @Override
    public Result getBindCardInfo(ManageBankCardInfo bankCardInfo) {
        return getResult(ResultEnum.SUCCESS, bankCardInfoMapper.selectOne(bankCardInfo));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result bindCard(ManageBankCardInfo bankCardInfo, SessionUserVo sessionUserVo) throws BizException {
        logger.info("绑定银行卡参数入参，bankCardInfo={}", JSON.toJSONString(bankCardInfo));
        Result result = getResult(ResultEnum.SUCCESS);
        try {
            //查找该银行卡信息
            ManageBankCardInfo findBankCardInfo = new ManageBankCardInfo();
            findBankCardInfo.setManagerId(sessionUserVo.getId());
            ManageBankCardInfo oldBankCardInfo = bankCardInfoMapper.selectOne(findBankCardInfo);

            if (oldBankCardInfo != null) {
                oldBankCardInfo.setCardNum(bankCardInfo.getCardNum());
                oldBankCardInfo.setBankName(bankCardInfo.getBankName());
                oldBankCardInfo.setIdCard(bankCardInfo.getIdCard());
                oldBankCardInfo.setOwnName(bankCardInfo.getOwnName());
                oldBankCardInfo.setUpdateTime(new Date());
                bankCardInfoMapper.updateByPrimaryKeySelective(oldBankCardInfo);
                //记录绑定日志
                ManageBankCardBind bankCardBind = new ManageBankCardBind();
                bankCardBind.setBankCardInfoId(oldBankCardInfo.getId());
                ManageBankCardBind bankCardBindEntity = bankCardBindMapper.selectOne(bankCardBind);
                bindCardLog(oldBankCardInfo, bankCardBindEntity.getId(), sessionUserVo, 1);
            } else {
                //保存银行卡信息
                Date date = new Date();
                bankCardInfo.setManagerId(sessionUserVo.getId());
                bankCardInfo.setBindPhone(sessionUserVo.getTelphone());
                bankCardInfo.setCreateTime(date);
                bankCardInfo.setUpdateTime(date);
                bankCardInfo.setCreateId(sessionUserVo.getId());
                bankCardInfoMapper.insertUseGeneratedKeys(bankCardInfo);

                //建立绑定关系
                ManageBankCardBind bankCardBind = new ManageBankCardBind();
                bankCardBind.setBankCardInfoId(bankCardInfo.getId());
                bankCardBind.setManagerId(sessionUserVo.getId());
                bankCardBind.setBindStatus(1);
                bankCardBind.setCreateTime(date);
                bankCardBind.setUpdateTime(date);
                bankCardBindMapper.insertUseGeneratedKeys(bankCardBind);
                //记录绑定日志
                bindCardLog(bankCardInfo, bankCardBind.getId(), sessionUserVo, 1);
            }
            return getResult(ResultEnum.SUCCESS);

        } catch (Exception ex) {
            logger.error("绑定银行卡异常,ex={}", ex);
            ex.printStackTrace();
            throw new BizException("绑定银行卡异常:12011");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result unBindCard(ManageBankCardInfo bankCardInfo, SessionUserVo sessionUserVo) throws BizException {
        logger.info("解绑银行卡参数入参，bankCardInfo={}", JSON.toJSONString(bankCardInfo));
        try {

            //解绑
            //删除银行卡信息
            ManageBankCardInfo oldBankCardInfo = bankCardInfoMapper.selectOne(bankCardInfo);

            //判断是否存在
            if (oldBankCardInfo == null) {
                return getResult(ResultEnum.NO_CHECKBIND_EXCEPTION);
            }
            bankCardInfoMapper.delete(oldBankCardInfo);

            //删除绑定信息
            ManageBankCardBind bankCardBind = new ManageBankCardBind();
            bankCardBind.setBankCardInfoId(bankCardInfo.getId());
            ManageBankCardBind bankCardBindEntity = bankCardBindMapper.selectOne(bankCardBind);
            bankCardBindMapper.delete(bankCardBindEntity);

            //记录解绑日志
            bindCardLog(oldBankCardInfo, bankCardBindEntity.getId(), sessionUserVo, 2);

            return getResult(ResultEnum.SUCCESS);

        } catch (Exception ex) {
            logger.error("解绑银行卡异常,ex={}", ex);
            ex.printStackTrace();
            throw new BizException("绑定银行卡异常:12012");
        }
    }

    public void bindCardLog(ManageBankCardInfo bankCardInfo, Long bindId, SessionUserVo sessionUserVo, int useType) {
        //记录绑定日志
        ManageBankCardLog bankCardLog = new ManageBankCardLog();
        bankCardLog.setCardNum(bankCardInfo.getCardNum());
        bankCardLog.setOwnName(bankCardInfo.getOwnName());
        bankCardLog.setBindPhone(bankCardInfo.getBindPhone());
        bankCardLog.setBankName(bankCardInfo.getBankName());
        bankCardLog.setIdCard(bankCardInfo.getIdCard());
        bankCardLog.setBindCardId(bindId);
        bankCardLog.setCreateId(sessionUserVo.getId());
        bankCardLog.setManagerId(sessionUserVo.getId());
        bankCardLog.setUseType(useType);
        bankCardLog.setCreateTime(new Date());
        bankCardLogMapper.insert(bankCardLog);
    }
}
